package com.cltech.controller.sys;

import com.cltech.dto.SysUserDto;
import com.cltech.entity.sys.SysExceptionLog;
import com.cltech.entity.sys.SysUser;
import com.cltech.frame.result.PageResult;
import com.cltech.frame.result.ResponseBean;
import com.cltech.frame.utils.JWTUtil;
import com.cltech.service.sys.SysPermissionService;
import com.cltech.service.sys.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * Created by Administrator on 2019/3/18.
 */
@RestController
@RequestMapping("api/user")
@Api(tags = {"用户管理"})
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    protected SysPermissionService sysPermissionService;
    @PostMapping
    @ApiOperation("新增或保存用户")
    public Object addOrUpdate(@RequestBody @Valid SysUserDto sysUserDto, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseBean.fail(bindingResult.getFieldError().getDefaultMessage());
        } else {
            return sysUserService.addOrUpdate(sysUserDto);
        }
    }
    @GetMapping
    @ApiOperation("用户列表查询")
    public Object list(HttpServletRequest request, @RequestParam(value="page",defaultValue="0")int page,
                       @RequestParam(value="size",defaultValue="10")int size) {
        return ResponseBean.success(PageResult.result(sysUserService.findAll(request.getParameterMap()
                ,page, size)));
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id获取用户")
    public Object get(@PathVariable String id) {
        return sysUserService.findById(id);
    }

    @GetMapping("/{id}/{state}")
    @ApiOperation("修改用户状态")
    public Object get(@PathVariable String id, @PathVariable String state) {
        return sysUserService.updateState(id,state);
    }

    @PostMapping("/changePassword")
    @ApiOperation("修改密码（验证旧密码）")
    public Object changePassword(String userId, String oldPassword, String password) {
        return null;
    }
    @PostMapping("/superChangePassword")
    @ApiOperation("修改密码")
    public Object changePassword(String userId, String password) {
        return null;
    }



    @GetMapping("/findPermissionsTreeByUser")
    @ApiOperation("查找用户权限资源树")
    public Object findPermissionsTreeByUser(String userId, String sysPermissionType, Integer sysPermissionlevel) {
        return sysPermissionService.findPermissionsTreeByUser(userId, sysPermissionType, sysPermissionlevel);
    }
}
